package cn.eshore.bnet.bpm.service.action;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.jbpm.graph.exe.ExecutionContext;
import org.jbpm.taskmgmt.def.AssignmentHandler;
import org.jbpm.taskmgmt.exe.Assignable;
import org.springframework.beans.factory.annotation.Required;
import org.springframework.stereotype.Component;

import cn.eshore.bnet.bpm.adapter.SecurityAdapter;

/**
 * 负责任务分配 分配一个任务需要的信息： 资源ID 节点ID（可选） 组织ID（可选）
 * 
 * @author zhoujinhuang
 */
@Component("securityTaskAssigment")
public class SecurityTaskAssigment implements AssignmentHandler {

    public SecurityAdapter securityAdapter;

    private static final long serialVersionUID = 5723332037883124836L;

    private static final Log LOG = LogFactory
            .getLog(SecurityTaskAssigment.class);

    public static final String NODE_ID = "NODE_ID";

    private static final String ORG_ID = "ORG_ID";

    private static final String IS_POOL = "IS_POOL";

    private static final String ROLE_ID = "ROLE_ID";

    /**
     * 根据 任务工单名称 节点ID 组织ID（可空）获取 操作员ID
     * 
     * @see org.jbpm.taskmgmt.def.AssignmentHandler#assign(org.jbpm.taskmgmt.exe.Assignable,
     *      org.jbpm.graph.exe.ExecutionContext)
     */
    public void assign(Assignable assignable, ExecutionContext executionContext)
            throws Exception {

        LOG.info(this.getClass().getName() + ".assign");
        // 任务工单名称
        String taskName;

        // 节点ID
        String nodeId;

        // 是否采用池方式分配任务
        boolean isAssignActorPool = false;

        // 组织ID
        String orgId;

        // 角色ID
        String roleId;
        // 任务工单处理人名称
        String assignerName;

        taskName = executionContext.getTaskInstance().getName();
        nodeId = (String) executionContext.getContextInstance().getVariable(
                NODE_ID);
        orgId = (String) executionContext.getContextInstance().getVariable(
                ORG_ID);
        roleId = (String) executionContext.getContextInstance().getVariable(
                ROLE_ID);
        String isPool = (String) executionContext.getContextInstance()
                .getVariable(IS_POOL);
        if (isPool != null) {
            isAssignActorPool = Boolean.parseBoolean(isPool);
        }
        StringBuffer sb = new StringBuffer();
        sb.append("SecurityTaskAssigment assign,");
        sb.append("taskName=").append(taskName).append(";");
        sb.append("nodeId=").append(nodeId).append(";");
        sb.append("orgId=").append(orgId).append(";");
        sb.append("roleId=").append(roleId).append(";");
        sb.append("isAssignActorPool=").append(isAssignActorPool).append(";");
        LOG.info(sb.toString());

        assignerName = securityAdapter.getActorId(
                taskName,
                nodeId,
                roleId,
                orgId,
                isAssignActorPool);
        assignable.setActorId(assignerName);
        LOG.info(" A new task of '" + taskName + "' is just assigned to ["
                + assignerName + "]. ");

    }

    public SecurityAdapter getSecurityAdapter() {
        return securityAdapter;
    }

    @Required
    public void setSecurityAdapter(SecurityAdapter securityAdapter) {
        this.securityAdapter = securityAdapter;
    }

}
